import { useReducer , useEffect, createContext } from 'react';
import PropTypes from 'prop-types';

const INIT_STATE = {
    address:null,
};

const reducer = (state: any,action: any) => {
    switch(action.type){
        case 'getAddress':
            return { ...state , address: action.payload };
        
        default:
            throw new Error(`Unknown type: ${action.type}`);
    }
}

const StoreContext = createContext<any>(null);

const StoreContextProvider = (props:any) => {
    // filtering props and merge with default param value
    const initState = { ...INIT_STATE };
    const [state, dispatch] = useReducer(reducer, initState);
  
    useEffect(()=>{
        dispatch({ type: 'getAddress', payload: '123456' });
        console.log(111)
    },[])
  
    return <StoreContext.Provider value={{ state, dispatch }}>{props.children}</StoreContext.Provider>
};

StoreContextProvider.propTypes = {
    socket: PropTypes.string,
    types: PropTypes.object
};

export { StoreContextProvider , StoreContext };